/* --- Copyright University of Sussex 1992. All rights reserved. ----------
 * File:            $popvision/lib/straight_hough.c
 * Purpose:         Straight line finding by Hough Transform
 * Author:          David Young, Nov 12 1992 (see revisions)
 * Documentation:   HELP *STRAIGHT_HOUGH
 * Related Files:   LIB *STRAIGHT_HOUGH
 */

/* 
 *  Poplog code without an explicit copyright is covered by the
 *  following copy-right:  
 * 
 * Copyright (C) 1981-1999 The University of Sussex. All Rights Reserved.
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:  
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software. 
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE UNIVERSITY OF SUSSEX BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE. 
 *
 * Copyright notices in the Poplog code and documentation files do not
 * restrict use of those files in accord with this notice, as the
 * notices remain solely on account of requirements of the
 * installation software used by the original development team. 
 *
 * This Poplog distribution is based at the University of Birmingham
 * solely as a service to users. 
 * 
 * THE UNIVERSITY OF BIRMINGHAM GIVES NO WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE UNIVERSITY OF BIRMINGHAM OR
 * ANY OF THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY CLAIM, DAMAGES
 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  
 */

/*
 * grabbed from: http://www.cs.bham.ac.uk/research/projects/poplog/popvision/lib/straight_hough.c
 */


#include <math.h>

void straight_hough_array_f(
    arr, xsize, xstart, xend, ystart, yend,
    hough, rsize, rstart, rend, tstart, tend,
    xbegin, ybegin, xincr, yincr, tbin)

float   *arr;
int     xsize, xstart, xend, ystart, yend;
float   *hough;
int     rsize, rstart, rend, tstart, tend;
/* The first pixel processed is at xbegin, ybegin in user coords */
float   xbegin, ybegin, xincr, yincr, tbin;

{   float   *arrslow, *arrfast, *arrslowend, *arrfastend,
            *h, *hbegin, *hend, *hh;
    int     xn = xend - xstart,
            thalf = rsize * (tend - tstart + 1);
    float   a, x, y, r,
            c, s, chold,
            cincr = cos(tbin), sincr = sin(tbin);
    int     rint;

    hbegin = hough + rsize * tstart + rstart;
    hend = hough + rsize * tend + rstart;

    for (arrslow = arr + xsize * ystart + xstart,
         arrslowend = arr + xsize * yend + xstart,
         y = ybegin;

         arrslow <= arrslowend;

         arrslow += xsize, y += yincr)

        for (arrfast = arrslow,
             arrfastend = arrslow + xn,
             x = xbegin;

             arrfast <= arrfastend;

             arrfast++, x += xincr)

            if ((a = *arrfast) != 0.0)
                for (h = hbegin,
                     c = 1,   s = 0;   /* First bin is at angle zero */

                     h <= hend;

                     chold = c * cincr - s * sincr,
                     s = s * cincr + c * sincr,
                     c = chold,
                     h += rsize) {
                    r = x * c + y * s;
                    if (r < 0.0) {
                        rint = -r;  /* truncate */
                        hh = h + thalf + rint; }
                    else {
                        rint = r;   /* truncate */
                        hh = h + rint; }
                    *hh += a; }
}

/* --- Revision History ---------------------------------------------------
--- David S Young, Nov 26 1992
        Installed
 */
